package com.attendance.www.hr.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.attendance.www.hr.beans.ApprovalRuleBean;
import com.attendance.www.hr.beans.ApprovalRulePageBean;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.IApprovalRuleService;
import com.zhangkongli.frame.core.ResultMap;
/**
 * 审批规则
 * @version 2017/10/26
 * @author 崔棋
 *
 */
@Controller
@RequestMapping("/approvalRule")
public class ApprovalRuleController {

	@Resource
	private IApprovalRuleService approvalRuleService;

	/**
	 * 获取全部审批规则接口
	 * @param request
	 * @param attendanceBean
	 * @return
	 */
	@RequestMapping(value = "/getApprovalRuleList.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getApprovalRuleList(HttpServletRequest request) {
		String pageNoStr=request.getParameter("pageNo");
		String pageCountStr=request.getParameter("pageCount");
		String requestTypeStr=request.getParameter("requestType");
		String requestRoleStr=request.getParameter("requestRole");
		if (StringUtils.isBlank(pageNoStr)||StringUtils.isBlank(pageCountStr)) {
			throw new ResultFaultException("0_00001");
		}
		int pageNo=Integer.parseInt(pageNoStr);
		int pageCount=Integer.parseInt(pageCountStr);
		Integer startNum = 0;
		if (pageNo > 1) {
			startNum = (pageNo - 1) * pageCount; 
		}
		ApprovalRuleBean approvalRuleBean=new ApprovalRuleBean();
		if(StringUtils.isNotBlank(requestTypeStr)){
			approvalRuleBean.setRequestType(Integer.parseInt(requestTypeStr));
		}
		if(StringUtils.isNotBlank(requestRoleStr)){
			approvalRuleBean.setRequestRole(Integer.parseInt(requestRoleStr));
		}
		ApprovalRulePageBean approvalRulePageBean=new ApprovalRulePageBean();
		approvalRulePageBean=approvalRuleService.getApprovalRulePageList(approvalRuleBean, startNum, pageCount);
		return ResultMap.success(approvalRulePageBean);
	}
	
	/**
	 * 添加审批规则接口			
	 * @param request
	 * @param punchTimeBean
	 * @return
	 */
	@RequestMapping(value = "/saveApprovalRule.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object saveApprovalRule(HttpServletRequest request, @RequestBody ApprovalRuleBean approvalRuleBean) {
		if (approvalRuleBean != null) {
			if (StringUtils.isBlank(approvalRuleBean.getRequestType()+"") 
					|| StringUtils.isBlank(approvalRuleBean.getRequestRole()+"")) {
				throw new ResultFaultException("0_00001");
			}
			if (approvalRuleService.saveApprovalRuleValidate(approvalRuleBean)) {
				throw new ResultFaultException("0_00010");
			} else {
				if (approvalRuleService.saveApprovalRule(approvalRuleBean)) {
					return ResultMap.success();
				} else {
					throw new ResultFaultException("0_00007");
				}
			}
		}
		throw new ResultFaultException("0_00001");
	}
	
	/**
	 * 修改审批规则接口			
	 * @param request
	 * @param punchTimeBean
	 * @return
	 */
	@RequestMapping(value = "/updateApprovalRule.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object updateApprovalRule(HttpServletRequest request, @RequestBody ApprovalRuleBean approvalRuleBean) {
		if (approvalRuleBean != null) {
			if (StringUtils.isBlank(approvalRuleBean.getApprovalRuleId()+"") 
					|| StringUtils.isBlank(approvalRuleBean.getRequestType()+"") 
					|| StringUtils.isBlank(approvalRuleBean.getRequestRole()+"")) {
				throw new ResultFaultException("0_00001");
			}
			if (approvalRuleService.updateApprovalRuleValidate(approvalRuleBean)) {
				throw new ResultFaultException("0_00010");
			} else {
				if (approvalRuleService.updateApprovalRule(approvalRuleBean)) {
					return ResultMap.success();
				} else {
					throw new ResultFaultException("0_00008");
				}
			}
		}
		throw new ResultFaultException("0_00001");
	}
	
	/**
	 * 删除审批规则接口			
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/deleteApprovalRule.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object deleteApprovalRule(HttpServletRequest request) {
		String approvalRuleIdString = request.getParameter("approvalRuleId");
		if (StringUtils.isBlank(approvalRuleIdString)) {
			throw new ResultFaultException("0_00001");
		}
		Integer approvalRuleId = Integer.valueOf(approvalRuleIdString);
		return ResultMap.success(approvalRuleService.deleteApprovalRule(approvalRuleId));
	}

}
